#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define INIT_SIZE 10   //初始化表的长度
#define DATA_TYPE int  //数据类型

typedef struct sqlist
{
    int len;           //当前实际数据的长度
    int max_len;       //保存当前表的总长度
    DATA_TYPE *data;   //需要动态分配
}sqlist,*p_sqlist;     //将struct sqlist * 类型 重命名为p_sqlist类型

//创建一个空表
int creat_sqlist(p_sqlist L)
{
    L->data=(DATA_TYPE *)malloc(sizeof(DATA_TYPE)*INIT_SIZE);//分配INIT_SIZE*sizeof(DATE_TYPE)空间给L->data
    if(L->data==NULL)
    {
        printf("malloc failed\n");
        return -1;
    }
    //将当前表中的数据初始为0
    memset(L->data,0,sizeof(DATA_TYPE)*INIT_SIZE);//清空数据为0
    //表的总允许长度
    L->max_len=INIT_SIZE;
    L->len=0;
    return 0;
}

//尾部添加一个数据
int tail_insert_data_tolist(DATA_TYPE data,p_sqlist L)
{
    //先判断是否为空表
    if(L->data==NULL)
    {
        printf("list is null\n");
        return -1;
    }
    //判断
    if(L->len>=L->max_len)
    {
        printf("list is full\n");
        return -1;
    }
    //插入数据
    L->data[L->len++]=data;
    return 0;
}

//删除一个数据
int pos_delet_data_fromlist(DATA_TYPE data,p_sqlist L)
{
    if(L->len==0||L->data==NULL)//空表
    {
       printf("list is null\n");
       return -1;
    }
    int i;
    for (i = 0; i < L->len; i++)
    {
        if(data==L->data[i])
        {
            break;
        }
    }
    //如果没有找到数据 表示改数据不在表中 返回-1
    if(i==L->len)
    {
        printf("data is not find\n");
        return -1;
    }
    for (; i <L->len-1; i++)
    {
        L->data[i]=L->data[i+1];
    }
    memset(&L->data[L->len-1],0,sizeof(DATA_TYPE));
    L->len--;
    return 0;
}
//遍历
int show_list(p_sqlist L)
{
    if(L->len==0||L->data==NULL)
    {
        printf("list is null\n");
        return -1;
    }
   
    printf("show list data:\n");
    for(int i=0;i<L->len;i++)
    {
        printf("%d ",L->data[i]);
    }
    printf("\n");
    return 0;
}
//扩容
int add_size_forlist(int size,p_sqlist L)
{
    L->data=(DATA_TYPE*)realloc(L->data,sizeof(DATA_TYPE)*(L->max_len+size));
    if (L->data==NULL)
    {
        printf("realloc faild\n");
        return -1;
    }
    
    L->max_len+=size;
    return 0;
}

int find_list(p_sqlist L,int data)
{
    int i;
    for (i = 0; i < L->len; i++)
    {
        if(L->data[i]==data)
        {
            return i;
        }
    }
    return -1;
}
//合并两个顺序表
int merge(p_sqlist L1,p_sqlist L2)
{
     //L1 判空
     if(L1->len==0||L1->data==NULL)
     {
       //  printf("L1 is null\n");
       //将L2给L1
       *L1=*L2;
       return 0;
     }
     if (L2->len==0||L2->data==NULL)
    {
        return 0;
    }
    for (size_t i = 0; i < L2->len; i++)
    {
        int result;
        result=find_list(L1,L2->data[i]);
        if(result==-1)
        {
            if(L1->len<L1->max_len)
            {
                tail_insert_data_tolist(L2->data[i],L1);
            }
            else
            {
              if(!add_size_forlist(1,L1))//扩容成功
              {
                tail_insert_data_tolist(L2->data[i],L1);
              }     
            }  
        }
    } 
    return 0;
}
//删除重复的数
int delet_list_redata(p_sqlist L)
{
    if(L==NULL)
    {
        printf("list is empty\n");
        return -1;
    }
    int i=0,j;
    for ( i = 0; i < L->len; i++)
    {
        for (j=i+1 ; j < L->len; j++)
        {
            if(L->data[i]==L->data[j])
            {
                pos_delet_data_fromlist(L->data[i],L);
                j=i;
            }
        }
        
    }
    return 0;
}
int main(int argc, char const *argv[])
{
    sqlist list1,list2;//结构体类型变量  
    creat_sqlist(&list1);//创建一个表
    tail_insert_data_tolist(1,&list1);
    tail_insert_data_tolist(2,&list1);
    tail_insert_data_tolist(1,&list1);
    tail_insert_data_tolist(3,&list1);
    tail_insert_data_tolist(1,&list1);
    tail_insert_data_tolist(2,&list1);
    tail_insert_data_tolist(1,&list1);
 


    creat_sqlist(&list2);
    tail_insert_data_tolist(1,&list2);
    tail_insert_data_tolist(3,&list2);
    tail_insert_data_tolist(5,&list2);
    tail_insert_data_tolist(7,&list2);
    tail_insert_data_tolist(9,&list2);
    tail_insert_data_tolist(11,&list2);
    tail_insert_data_tolist(12,&list2);
    tail_insert_data_tolist(13,&list2);
    tail_insert_data_tolist(14,&list2);
    tail_insert_data_tolist(15,&list2);

    merge(&list1,&list2);
    show_list(&list1);
    printf("\n");
    delet_list_redata(&list1);
    show_list(&list1);

    return 0;
}
